package qmm.dao.impl;
package qmm.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qmm.dao.BaseOrderDao;
import qmm.entity.BaseOrder;
import qmm.entity.vo.CartVo;
import qmm.util.JdbcUtil;

public class BaseOrderDaoImpl implements BaseOrderDao {

	@Override
	public int add(BaseOrder order) {
		String sql = "INSERT INTO t_base_order ( user_id,  money, order_no,status,  address_id) VALUES ( "+order.getUserId()+",  "+order.getMoney()+", upper(replace(uuid(),'-','')), "+order.getStatus()+","+order.getAddressId()+")";
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int getId(int userId) {
		int n = 0;
		String sql = "select max(id) from t_base_order where user_id="+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<BaseOrder> findByUserId(int userId) {
		List<BaseOrder> list = new ArrayList<BaseOrder>();
		String sql = "select * from t_base_order where user_id = "+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				Timestamp createTime = rs.getTimestamp("create_time");
				String orderNo = rs.getString("order_no");
				double money = rs.getDouble("money");
				int status = rs.getInt("status");
				int addressId = rs.getInt("address_id");
				BaseOrder order = new BaseOrder(id, userId, createTime, money, orderNo, status, addressId);
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int cancel(int id) {
		String sql = "update t_base_order set status=5 where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int confirm(int id) {
		String sql = "update t_base_order set status=4 where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public List<BaseOrder> findAll(String orderNo,int page, int size) {
		int n = (page-1)*size;
		List<BaseOrder> list = new ArrayList<BaseOrder>();
		String sql ="select o.*,concat(a.province,a.city,a.district,a.address,' ',a.receiver,' ',a.phone) address from t_base_order o inner join t_user_address a on o.address_id=a.id where 1=1";
		if(orderNo!=null && orderNo!=""){
			sql+=" and order_no='"+orderNo+"'";
		}
		sql += " limit "+n+","+size;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				int userId = rs.getInt("user_id");
				Timestamp createTime = rs.getTimestamp("create_time");
				String orderNo2 = rs.getString("order_no");
				String address = rs.getString("address");
				String waybill = rs.getString("waybill");
				double money = rs.getDouble("money");
				int status = rs.getInt("status");
				int addressId = rs.getInt("address_id");
				BaseOrder order = new BaseOrder(id, userId, createTime, money, orderNo2, status, addressId,address,waybill);
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int getCount(String orderNo) {
		int n = 0;
		String sql ="select count(*) from t_base_order where 1=1";
		if(orderNo!=null && orderNo!=""){
			sql+=" and order_no='"+orderNo+"'";
		}
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int send(int id, String waybill) {
		String sql = "update t_base_order set status=3, waybill='"+waybill+"' where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

}
 java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qmm.dao.BaseOrderDao;
import qmm.entity.BaseOrder;
import qmm.entity.vo.CartVo;
import qmm.util.JdbcUtil;

public class BaseOrderDaoImpl implements BaseOrderDao {

	@Override
	public int add(BaseOrder order) {
		String sql = "INSERT INTO t_base_order ( user_id,  money, order_no,status,  address_id) VALUES ( "+order.getUserId()+",  "+order.getMoney()+", upper(replace(uuid(),'-','')), "+order.getStatus()+","+order.getAddressId()+")";
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int getId(int userId) {
		int n = 0;
		String sql = "select max(id) from t_base_order where user_id="+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<BaseOrder> findByUserId(int userId) {
		List<BaseOrder> list = new ArrayList<BaseOrder>();
		String sql = "select * from t_base_order where user_id = "+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				Timestamp createTime = rs.getTimestamp("create_time");
				String orderNo = rs.getString("order_no");
				double money = rs.getDouble("money");
				int status = rs.getInt("status");
				int addressId = rs.getInt("address_id");
				BaseOrder order = new BaseOrder(id, userId, createTime, money, orderNo, status, addressId);
				list.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int cancel(int id) {
		String sql = "update t_base_order set status=5 where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int confirm(int id) {
		String sql = "update t_base_order set status=4 where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

}
